﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Configuration;
using Microsoft.WindowsAzure;

    namespace LoadController
    {
        public class AppSettings : BaseAppSettings
        {
            public static string Table_FinishedJobs
            {
                get
                {
                    try
                    {
                        return CloudConfigurationManager.GetSetting("Table_FinishedJobs");
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.ToString());
                        return null;
                    }
                    
                }
            }

            public static string Table_Queue
            {
                get
                {
                    try
                    {
                        return CloudConfigurationManager.GetSetting("Table_Queue");
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.ToString());
                        return null;
                    }
                }
            }

            public static string Table_InvokeSamples
            {
                get
                {
                    try
                    {
                        return CloudConfigurationManager.GetSetting("Table_InvokeSamples");
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.ToString());
                        return null;
                    }
                }
            }

            public static string BlobContainer_results
            {
                get
                {
                    try
                    {
                        return CloudConfigurationManager.GetSetting("BlobContainer_results");
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.ToString());
                        return null;
                    }
                }
            }

            public static string AutoScaler_subscriptionname
            {
                get
                {
                    try
                    {
                        return CloudConfigurationManager.GetSetting("AutoScaler_subscriptionname");
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.ToString());
                        return null;
                    }
                }
            }

            public static string AutoScaler_managementcertificatethumbprint
            {
                get
                {
                    try
                    {
                        return CloudConfigurationManager.GetSetting("AutoScaler_managementcertificatethumbprint");
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.ToString());
                        return null;
                    }
                }
            }

            public static string AutoScaler_subscriptionid
            {
                get
                {
                    try
                    {
                        return CloudConfigurationManager.GetSetting("AutoScaler_subscriptionid");
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.ToString());
                        return null;
                    }
                }
            }

            public static string AutoScaler_hostedservicednsprefix
            {
                get
                {
                    try
                    {
                        return CloudConfigurationManager.GetSetting("AutoScaler_hostedservicednsprefix");
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.ToString());
                        return null;
                    }
                }
            }

            public static string AutoScaler_targetrolename
            {
                get
                {
                    try
                    {
                        return CloudConfigurationManager.GetSetting("AutoScaler_targetrolename");
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.ToString());
                        return null;
                    }
                }
            }

            public static void CreateTablesIfNotExist()
            {
                BaseAppSettings.CreateTablesIfNotExistBase(typeof(AppSettings));
            }

            public static void CreateBlobContainersIfNotExist()
            {
                BaseAppSettings.CreateBlobContainersIfNotExistBase(typeof(AppSettings));
            }
        }
    }

    namespace LoadGenerator
    {
        public class AppSettings : BaseAppSettings
        {
            public static void CreateTablesIfNotExist()
            {
                BaseAppSettings.CreateTablesIfNotExistBase(typeof(AppSettings));
            }

            public static void CreateBlobContainersIfNotExist()
            {
                BaseAppSettings.CreateBlobContainersIfNotExistBase(typeof(AppSettings));
            }
        }

    }

    public abstract class BaseAppSettings
    {
        public static string Table_ActiveJob
        {
            get
            {
                try
                {
                    return CloudConfigurationManager.GetSetting("Table_ActiveJob");
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.ToString());
                    return null;
                }
            }
        }

        public static string Table_Samples
        {
            get
            {
                try
                {
                    return CloudConfigurationManager.GetSetting("Table_Samples");
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.ToString());
                    return null;
                }
            }
        }

        public static string BlobContainer_assemblies
        {
            get
            {
                try
                {
                    return CloudConfigurationManager.GetSetting("BlobContainer_assemblies");
                    }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.ToString());
                    return null;
                }
            }
        }

        protected static void CreateTablesIfNotExistBase(Type t)
        {
            foreach (PropertyInfo pi in t.GetProperties().Where(property => property.Name.StartsWith("Table_")))
            {
                string tablename = pi.GetValue(null, null).ToString();
                AzureTableHelper.CreateTable(tablename);
            }

            foreach (PropertyInfo pi in typeof(BaseAppSettings).GetProperties().Where(property => property.Name.StartsWith("Table_")))
            {
                string tablename = pi.GetValue(null, null).ToString();
                AzureTableHelper.CreateTable(tablename);
            }
        }

        protected static void CreateBlobContainersIfNotExistBase(Type t)
        {
            foreach (PropertyInfo pi in t.GetProperties().Where(property => property.Name.StartsWith("BlobContainer_")))
            {
                string containerName = pi.GetValue(null, null).ToString();
                AzureBlobHelper.CreateBlobContainer(containerName);
            }

            foreach (PropertyInfo pi in typeof(BaseAppSettings).GetProperties().Where(property => property.Name.StartsWith("BlobContainer_")))
            {
                string containerName = pi.GetValue(null, null).ToString();
                AzureBlobHelper.CreateBlobContainer(containerName);
            }
        }

    }